package com.service;

/**********************************************
 MODIFY HISTORY 
 NO.01 LIN.XIN  add  fm_mdl_id_fk , cut_mdl_id_fk 20130812
 NO.02 LIN.XIN  add  from_thickness,to_thickness 20140320 
 *********************************************************/
import static com.def.GenericDef.E_XPAPLYSO_BIS_PATH_NOT_FOUND;
import static com.def.GenericDef.E_XPAPLYSO_CLOSE_STAT;
import static com.def.GenericDef.E_XPAPLYSO_OTHER_ERROR;
import static com.def.GenericDef.E_XPAPLYSO_OUT_OF_QTY;
import static com.def.GenericDef.E_XPAPLYSO_PLN_PRD_QTY_ERROR;
import static com.def.GenericDef.E_XPAPLYSO_RET_WO_NOT_FOUND;
import static com.def.GenericDef.E_XPAPLYSO_SALES_ORDER_ALREADY_EXIST;
import static com.def.GenericDef.E_XPAPLYSO_WO_ALREADY_RELEASED;
import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.def.GenericDef.RETURN_MESG_OK;
import static com.def.GenericDef.STRING_SERIAL_TYPE_SO;
import static com.def.GenericDef.WO_STATUS_CLOS;
import static com.def.GenericDef.WO_STATUS_WAIT;
import static com.def.GenericDef.T_AMSTHICKNESS;
import static com.def.GenericDef.TRX_IN;
import static com.def.GenericDef.PROC_RELS;
import static com.def.GenericDef._YES;
import static com.def.GenericDef._NO;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import com.dao.IBisMdlDefDao;
import com.dao.IRetCusSoDao;
import com.dao.IRetPrdInDao;
import com.dao.IRetPrdInfoDao;
import com.dao.IRetWODao;
import com.icim.service.ICIMBaseService;
import com.model.Bis_mdl_def;
import com.model.Bis_pathi;
import com.model.Ret_cus_so;
import com.model.Ret_prd_in;
import com.model.Ret_prd_info;
import com.model.Ret_wo;
import com.tx.Amsthickness.AmsthicknessI;
import com.tx.Xpaplyso.XpaplysoI;
import com.tx.Xpaplyso.XpaplysoIA;
import com.tx.Xpaplyso.XpaplysoIB;
import com.tx.Xpaplyso.XpaplysoO;
import com.tx.Xpaplyso.XpaplysoOA;
import com.tx.Xpcrtno.XpcrtnoI;
import com.tx.Xpcrtno.XpcrtnoO;
import com.util.MqOpe;
import com.util.Util;

@Scope("prototype")
@Service("XpaplysoService")
public class XpaplysoService extends ICIMBaseService{

	@Autowired
	private MqOpe mqOpe;
	
	@Autowired
	private IRetWODao retWODao;

	@Autowired
	private IBisMdlDefDao bisMdlDefDao;
	
	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;
	
	@Autowired
	private IRetPrdInDao retPrdInDao;
	
	@Autowired
	private IRetCusSoDao retCusSoDao;
	
	@Autowired
	private SendMsgService sendMsgService;
	
	@Autowired
	XpaplysoI inTrx;

	@Autowired
	XpaplysoO outTrx;

	@Autowired
	private XpcrtnoService XpcrtnoService;
	
	private Logger logger = Logger.getLogger(XpaplysoService.class);

	/****** 宏定义 ******/
	public final static String WO_TYPE_SALES_ORDER = "S";

	private static final String OPEN_WO = "Y";
	private static final String CLOSE_WO = "X";

	public String subMainProc(String strInMsg) {
		String reply = "";
		try {
			inTrx = (XpaplysoI) mqOpe.inStr2Obj(strInMsg, inTrx);
			outTrx.setTrx_id("XPAPLYSO");
			outTrx.setType_id("O");
			logger.info("InTrx is " + strInMsg);

			String actionFlg = inTrx.getAction_flg();
			if (actionFlg.equals("Q")) {
				 getWoProc();
			} else if (actionFlg.equals("D")) {
				delWoProc();
			} else if (actionFlg.equals("U")) {
				outTrx = updWoProc(inTrx);
			} else if (actionFlg.equals("A")) {
				addWoProc(inTrx);
			} else if (actionFlg.equals("W")) {
				outTrx = getAllWoProc(inTrx);
			} else if (actionFlg.equals("G")) {// Get wo for so binding
				outTrx = getSoForWoProc();
			} else if (actionFlg.equals("B")) {
				BindingSoForWoProc();
			} else if (actionFlg.equals("S")) {// get so for wo
				getSoForWoByMtrlProc();
			} else if (actionFlg.equals("O")) {
				bindrealSoForWoProc();
			} else if (actionFlg.equals(CLOSE_WO)) {
				closeWOProc();
			} else if (actionFlg.equals(OPEN_WO)) {
				openWOProc();
			} else if (actionFlg.equals("C")){//查询客户订单（未登记的）
				queryCusSoProc();
			}
			if ((RETURN_CODE_OK).equals(outTrx.getRtn_code())) {
				retWODao.flushCurSession();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		} catch (Exception ex) {
			logger.error(Util.stackTraceToString(ex));
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(ex.toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} finally {
			reply = mqOpe.obj2Str(outTrx);
		}
		return reply;

	}
	private String getStatDsc(String woStat){
		if(woStat.equals("CLOS")){
			return "关闭";
		}else if(woStat.equals("WAIT")){
			return "等待";
		}else if(woStat.equals("INPR")){
			return "制程中";
		}else if(woStat.equals("HOLD")) {
			 return "锁定";
		}
		return "其他";
	}

	public void  getWoProc() {
		outTrx.setTrx_id("XPAPLYSO");
		outTrx.setType_id("O");
		List<XpaplysoOA> oaryList = new ArrayList<XpaplysoOA>();
		try {
			XpaplysoIA iary = inTrx.getIary().get(0);
			String wo_id = iary.getWo_id();
			String wo_dsc = iary.getWo_dsc();
			String wo_prty = iary.getWo_prty();
			String mdl_id = iary.getMdl_id();
			String wo_cate = iary.getWo_cate();
			String wo_stat = iary.getWo_stat();
			String cus_id = iary.getCus_id();
			String mtrl_prod_id = iary.getMtrl_prod_id();
			String th_mdl_id_fk = iary.getTh_mdl_id_fk();
			String fm_mdl_id_fk = iary.getFm_mdl_id_fk();
			String cut_mdl_id_fk = iary.getCut_mdl_id_fk();
			String pln_stb_date = iary.getPln_stb_date();
			Date date_pln_stb_date = null;
			if(pln_stb_date != null){
				date_pln_stb_date = java.sql.Date.valueOf(pln_stb_date);
	    	}
			
			String sql = "FROM Ret_wo where wo_typ='S' ";
			if (wo_id != null) {
				sql += " AND id.wo_id='" + wo_id + "'";
			} else {
				if (wo_dsc != null) {
					sql += " AND wo_dsc like %" + wo_dsc + "'%'";
				}
				if (wo_prty != null) {
					sql += " AND wo_prty='" + wo_prty + "'";
				}
				if (mdl_id != null) {
					sql += " AND mdl_id_fk='" + mdl_id + "'";
				}
				if(mtrl_prod_id!=null){
					sql += " AND mtrl_prod_id_fk ='" + mtrl_prod_id + "'";
				}
				if (wo_cate != null) {
					sql += " AND wo_cate='" + wo_cate + "'";
				}
				if(wo_stat!=null){
					sql += " AND wo_stat='"+wo_stat+"'";
				}
				if(cus_id!=null){
					sql += " AND cus_id_fk = '" + cus_id + "'";
				}
				if(th_mdl_id_fk!=null){
					sql +=" and th_mdl_id_fk ='" + th_mdl_id_fk +"'";
				}
				if(fm_mdl_id_fk!=null){
					sql +=" and fm_mdl_id_fk ='" + fm_mdl_id_fk +"'";
				}
				if(cut_mdl_id_fk!=null){
					sql +=" and cut_mdl_id_fk ='" + cut_mdl_id_fk +"'";
				}
				if(date_pln_stb_date!=null){
					sql +=" and pln_stb_date='" + date_pln_stb_date + "'";
				}
			}
			List<Ret_wo> woList = retWODao.find(sql);

			if (woList != null) {
				for (Ret_wo ret_wo : woList) {
					XpaplysoOA oary = new XpaplysoOA();
					//bind wo
					String hql = "FROM Ret_wo where wo_typ='W' and so_id='" + ret_wo.getWo_id() + "'";
					List<Ret_wo> retWOList = retWODao.find(hql);
					if(retWOList.size()!=0){
                       oary.setBnd_wo(retWOList.get(0).getWo_id());
					}
					//end
					oary.setWo_id(ret_wo.getWo_id());
					oary.setWo_cate(ret_wo.getWo_cate());
					if (ret_wo.getPln_stb_date() != null) {
						oary.setPln_stb_date(ret_wo.getPln_stb_date().toString().substring(0, 10));
					}
					if (ret_wo.getPln_cmp_date() != null) {
						oary.setPln_cmp_date(ret_wo.getPln_cmp_date().toString().substring(0, 10));
					}
					oary.setWo_prty(ret_wo.getWo_prty());
					oary.setWo_stat(getStatDsc(ret_wo.getWo_stat()));
					oary.setDest_shop(ret_wo.getDest_shop());
					oary.setMdl_id(ret_wo.getMdl_id_fk());
					oary.setPln_prd_qty(ret_wo.getPln_prd_qty());
					oary.setWo_dsc(ret_wo.getWo_dsc());
					oary.setOpe_eff_flg(ret_wo.getOpe_eff_flg());
					oary.setVcr_flg_fk(ret_wo.getVcr_flg_fk());
					oary.setEvt_usr(ret_wo.getEvt_usr());
					oary.setCrt_usr(ret_wo.getCrt_usr());
					oary.setMtrl_part(ret_wo.getMtrl_part());
					if (ret_wo.getEvt_timestamp() != null) {
						oary.setEvt_timestamp(ret_wo.getEvt_timestamp().toString());
					}
					if (ret_wo.getCrt_timestamp() != null){
						oary.setCrt_timestamp(ret_wo.getCrt_timestamp());
					}
					oary.setMtrl_prod_id(ret_wo.getMtrl_prod_id_fk());
					oary.setWo_note(ret_wo.getWo_note());
					
					oary.setTh_mdl_id_fk(ret_wo.getTh_mdl_id_fk());
					oary.setFm_mdl_id_fk(ret_wo.getFm_mdl_id_fk());// NO.01
					oary.setCut_mdl_id_fk(ret_wo.getCut_mdl_id_fk());// NO.01
					oary.setCus_id(ret_wo.getCus_id_fk());
					oary.setSo_id(ret_wo.getSo_id());
					//NO.02
					if (ret_wo.getFrom_thickness() != null) {
						oary.setFrom_thickness(ret_wo.getFrom_thickness().toString());
					}
					//NO.02
					if (ret_wo.getTo_thickness() != null) {
						oary.setTo_thickness(ret_wo.getTo_thickness().toString());
					}
					//NO.02
					Bis_mdl_def bis_mdl_def = bisMdlDefDao.get(ret_wo.getMdl_id_fk());
					if (bis_mdl_def != null) {
						oary.setMdl_size(bis_mdl_def.getMdl_size());
						oary.setMdl_dsc(bis_mdl_def.getMdl_dsc());
						oary.setLayot_id_fk(bis_mdl_def.getLayot_id_fk());
					}
					oary.setPath_id(ret_wo.getPath_id_fk());
					oary.setPath_ver(ret_wo.getPath_ver_fk());
					oary.setCus_info_fst(ret_wo.getCus_info_fst());
					oary.setCus_info_snd(ret_wo.getCus_info_snd());
					oary.setPln_stb_timestamp(ret_wo.getPln_stb_timestamp());
					oary.setPln_cmp_timestamp(ret_wo.getPln_cmp_timestamp());
					oary.setManual(ret_wo.getManual());
					oary.setCus_mtrl_id_fk(ret_wo.getCus_mtrl_id_fk());
					oary.setGlass_from(ret_wo.getGlass_from());
					if(ret_wo.getT_thickness() != null){
						oary.setT_thickness(ret_wo.getT_thickness().toString());
					}else {
						oary.setT_thickness("");
					}
					if(ret_wo.getT_thickness() != null){
						oary.setC_thickness(ret_wo.getC_thickness().toString());
					}else {
						oary.setC_thickness("");
					}
					
					oaryList.add(oary);
				}
			}
		} catch (Exception ex) {
			logger.error(Util.stackTraceToString(ex));
			outTrx.setRtn_code(E_XPAPLYSO_OTHER_ERROR + "");
			outTrx.setRtn_mesg(ex.getMessage());
			return;
		}

		outTrx.setRtn_code("0000000");
		outTrx.setRtn_mesg("success");
		outTrx.setOary(oaryList);
		outTrx.setTbl_cnt(oaryList.size());
	}

	public void addWoProc(XpaplysoI xpaplysoI) {
		String swh_cnt_flg = _NO;
		String updateCrtNOFlg;
		outTrx.setTrx_id("XPAPLYSO");
		outTrx.setType_id("O");
		long tbl_cnt = xpaplysoI.getTbl_cnt();
		for (int i = 0; i < tbl_cnt; i++) {
			Ret_wo ret_wo = new Ret_wo();
			XpaplysoIA iary = xpaplysoI.getIary().get(i);
			ret_wo.setWo_id(iary.getWo_id());
			if (retWODao.get(iary.getWo_id()) != null) {
				outTrx.setRtn_code(E_XPAPLYSO_SALES_ORDER_ALREADY_EXIST + "");
				outTrx.setRtn_mesg("Sales Order already existed,Please check!!");
				return ;
			}
			ret_wo.setWo_typ(WO_TYPE_SALES_ORDER);
			ret_wo.setWo_prty(iary.getWo_prty());
			ret_wo.setWo_dsc(iary.getWo_dsc());
			ret_wo.setMdl_id_fk(iary.getMdl_id());
			ret_wo.setWo_cate(iary.getWo_cate());
			ret_wo.setWo_prty(iary.getWo_prty());
			ret_wo.setMtrl_part(iary.getMtrl_part());
			ret_wo.setPln_stb_date(Date.valueOf(iary.getPln_stb_date()));
			ret_wo.setPln_stb_timestamp(Timestamp.valueOf(iary.getPln_stb_date()+" 00:00:00"));
			if(iary.getPln_cmp_date()!=null){
				ret_wo.setPln_cmp_date(Date.valueOf(iary.getPln_cmp_date()));
				ret_wo.setPln_cmp_timestamp(Timestamp.valueOf(iary.getPln_cmp_date()+" 00:00:00"));
			}
			ret_wo.setPln_prd_qty(Integer.parseInt(iary.getPln_prd_qty()));
			ret_wo.setOpe_eff_flg(iary.getOpe_eff_flg());
			ret_wo.setEvt_usr(iary.getEvt_user());
			ret_wo.setEvt_timestamp(Util.getCurrentTimestamp());
			String sql = "FROM Bis_pathi where id.path_id='" + iary.getPath_id() + "' and "
					+ "id.path_ver='" + iary.getPath_ver() + "'";
			Bis_pathi bis_pathi = (Bis_pathi) bisMdlDefDao.comTblRead(sql);
			if (bis_pathi == null) {
				outTrx.setRtn_code(E_XPAPLYSO_BIS_PATH_NOT_FOUND + "");
				outTrx.setRtn_mesg("["+iary.getMdl_id()+"]工艺路线缺失,请在6400界面重新维护此产品信息");
				return ;
			}
			if (!ret_wo.getWo_cate().equals("RMA")) {
				ret_wo.setPath_id_fk(iary.getPath_id());
				ret_wo.setPath_ver_fk(iary.getPath_ver());
				ret_wo.setStr_ope_no(bis_pathi.getFirst_ope_no());
			}
			if(null != iary.getManual()){
				ret_wo.setManual(iary.getManual());
			}
			if(null != iary.getCusMtrl()){
				ret_wo.setCus_mtrl_id_fk(iary.getCusMtrl());
			}
			if(null != iary.getGlassFrom()){
				ret_wo.setGlass_from(iary.getGlassFrom());
			}
			ret_wo.setMdl_cate_fk(bis_pathi.getMdl_cate_fk());//订单的mdl_cate是从pathi中的mdl_cate获取
			ret_wo.setRl_prd_qty(0);
			ret_wo.setComp_prd_qty(0);
			ret_wo.setWh_in_prd_qty(0);
			ret_wo.setScrp_prd_qty(0);
			ret_wo.setWip_wh_in_prd_qty(0);
			ret_wo.setRsv_dps_qty(0);
			ret_wo.setRsv_pln_prd_qty(0);
			ret_wo.setWh_out_prd_qty(0);
			ret_wo.setRsv_dn_qty(0);
			ret_wo.setAct_dps_qty(0);
			ret_wo.setWip_wh_out_prd_qty(0);
			ret_wo.setRsv_snd_dps_qty(0);
			ret_wo.setAct_snd_dps_qty(0);
			ret_wo.setRsv_trd_dps_qty(0);
			ret_wo.setAct_trd_dps_qty(0);
			ret_wo.setRsv_fth_dps_qty(0);
			ret_wo.setAct_fth_dps_qty(0);
			ret_wo.setVcr_flg_fk(iary.getVcr_flg_fk());
			ret_wo.setWo_stat("WAIT");
			ret_wo.setPlant_id("TODO");// TODO
			ret_wo.setEvt_usr(iary.getEvt_user());
			ret_wo.setMtrl_prod_id_fk(iary.getMtrl_prod_id());
			ret_wo.setTh_mdl_id_fk(iary.getTh_mdl_id_fk());
			ret_wo.setFm_mdl_id_fk(iary.getFm_mdl_id_fk());// NO.01
			ret_wo.setCut_mdl_id_fk(iary.getCut_mdl_id_fk());// NO.01
			ret_wo.setWo_note(iary.getWo_note());
			ret_wo.setFrom_thickness(Double.valueOf(iary.getFrom_thickness()));//NO.02
			ret_wo.setTo_thickness(Double.valueOf(iary.getTo_thickness()));//NO.02
			ret_wo.setCus_id_fk(iary.getCus_id());
			ret_wo.setCrt_timestamp(Util.getCurrentTimestamp());
			ret_wo.setCrt_usr(iary.getEvt_user());
			ret_wo.setSo_id(iary.getWo_id());
			ret_wo.setT_thickness(Double.valueOf(iary.getT_thickness()));
			ret_wo.setC_thickness(Double.valueOf(iary.getC_thickness()));
			ret_wo.setWh_rwk_qty(0);
			ret_wo.setCus_rma_qty(0);
			if ("".equals(iary.getCus_info_fst())){
				ret_wo.setCus_info_fst("");
			}else {
				ret_wo.setCus_info_fst(iary.getCus_info_fst());
			}
			
			if ("".equals(iary.getCus_info_snd())){
				ret_wo.setCus_info_snd("");
			}else {
				ret_wo.setCus_info_snd(iary.getCus_info_snd());
			}
			
			/**
			 * 根据so id查询RET_CUS_SO.设定swh_cnt_flg
			 */
			Ret_cus_so cusSo = retCusSoDao.get(iary.getWo_id());
			if(cusSo != null){
				swh_cnt_flg = _YES;
				cusSo.setCus_so_stat(PROC_RELS);
				retCusSoDao.save(cusSo);
			}else{
				swh_cnt_flg = _NO;
			}
			
			if(_YES.equals(swh_cnt_flg)){
				ret_wo.setSwh_cnt_flg(swh_cnt_flg);
			}
			retWODao.save(ret_wo);
			
			List<XpaplysoIB> iaryBList = iary.getIaryB();
			if (iaryBList != null) {
				if (iaryBList.size() > 0) {
					for (XpaplysoIB iaryB : iaryBList) {
						Ret_wo ret_so = retWODao.get(iaryB.getWo_id());
						if (ret_so == null) {
							outTrx.setRtn_code(E_XPAPLYSO_RET_WO_NOT_FOUND + "");
							outTrx.setRtn_mesg("not Found wo [" + iaryB.getWo_id() + "]");
							return ;
						}
						if(_YES.equals(swh_cnt_flg)){
							ret_so.setSwh_cnt_flg(swh_cnt_flg);
						}
						ret_so.setSo_id(iary.getWo_id());
						retWODao.update(ret_so);
					}
				}
			}
			updateCrtNOFlg = iary.getUpdate_crt_no_flg() != null ? iary.getUpdate_crt_no_flg() : "";
			if (updateCrtNOFlg.equals("Y")) {
				MqOpe mqOpe = new MqOpe();
				Calendar calendar = Calendar.getInstance();
				String year = calendar.get(Calendar.YEAR) + "";
				XpcrtnoI xpcrtNOI = new XpcrtnoI();
				String woId = iary.getWo_id();
				if(woId.contains("DQR"+year)){
					String serialNo = woId.substring(7);
					xpcrtNOI = new XpcrtnoI();
					xpcrtNOI.setTrx_id("XPCRTNO");
					xpcrtNOI.setType_id("I");
					xpcrtNOI.setType(STRING_SERIAL_TYPE_SO);
					xpcrtNOI.setKey_id(year);
					xpcrtNOI.setAction_flg("U");
					xpcrtNOI.setSerial_no(serialNo);
					String strInMsg = mqOpe.obj2Str(xpcrtNOI);
					String strOutMsg = XpcrtnoService.subMainProc(strInMsg);
					XpcrtnoO xpcrtNoO = new XpcrtnoO();
					xpcrtNoO = (XpcrtnoO) mqOpe.inStr2Obj(strOutMsg, xpcrtNoO);
					if (!xpcrtNoO.getRtn_code().equals(RETURN_CODE_OK)) {
						outTrx.setRtn_code("自动同步订单序列号失败");
						outTrx.setRtn_mesg(xpcrtNoO.getRtn_mesg());
						return ;
					}
				}
				
			}

		}
		getWoProc();
	}

	public void delWoProc() {
		for (XpaplysoIA iary : inTrx.getIary()) {
			String soId = iary.getWo_id();
			String hql = "from Ret_prd_info where so_id_fk = ?";
			List<Ret_prd_info> retPrdInfoList = retPrdInfoDao.list(hql, soId);
			if(!retPrdInfoList.isEmpty()){
				outTrx.setRtn_code(E_XPAPLYSO_WO_ALREADY_RELEASED+"");
				outTrx.setRtn_mesg("订单已经投产下线,不能被删除");
				return;
			}
			hql = "from Ret_prd_in where so_id_fk = ?";
			List<Ret_prd_in> retPrdInList = retPrdInDao.list(hql, soId);
			if(!retPrdInList.isEmpty()){
				outTrx.setRtn_code(E_XPAPLYSO_WO_ALREADY_RELEASED+"");
				outTrx.setRtn_mesg("订单已经发料,不能被删除");
				return;
			}
			hql ="from Ret_wo where so_id = ? and wo_typ='W'";
			List<Ret_wo> retWoList = retWODao.list(hql, soId);
			if(!retWoList.isEmpty()){
				outTrx.setRtn_code(E_XPAPLYSO_WO_ALREADY_RELEASED+"");
				outTrx.setRtn_mesg("订单已经和内部订单["+retWoList.get(0).getWo_id()+"] 绑定 不能被删除");
				return;
			}
				
			Ret_wo ret_wo = retWODao.get(iary.getWo_id());
			retWODao.delete(ret_wo);
			
			Ret_cus_so cusSo = retCusSoDao.get(iary.getWo_id());
			if(cusSo != null){
				cusSo.setCus_so_stat("INIT");
				retCusSoDao.save(cusSo);
			}
		}
		outTrx.setRtn_code("0000000");
		outTrx.setRtn_mesg("success");
	}
	private boolean checkSoPlnPrdQty(Ret_wo retSo){
		String hql ="FROM Ret_wo where wo_typ='W' and so_id = '"+retSo.getWo_id()+"'";
		int soPlnPrdQty = retSo.getPln_prd_qty();
		int totalWOsPlnPrdQty = 0;
		List<Ret_wo> retWoList = retWODao.find(hql);
		for(Ret_wo retWo:retWoList){
			totalWOsPlnPrdQty = totalWOsPlnPrdQty + retWo.getRsv_pln_prd_qty();
		}
		if(soPlnPrdQty<totalWOsPlnPrdQty){
			return false;
		}
		return true;
	}
	public XpaplysoO updWoProc(XpaplysoI xpaplysoI) {
		XpaplysoO xpaplysoO = new XpaplysoO();
		xpaplysoO.setTrx_id("XPAPLYSO");
		xpaplysoO.setType_id("O");
		long tbl_cnt = xpaplysoI.getTbl_cnt();
		for (int i = 0; i < tbl_cnt; i++) {
			XpaplysoIA iary = xpaplysoI.getIary().get(i);
			String soId = iary.getWo_id();
			Ret_wo retSo = retWODao.get(soId);
			Double fromThicknessBefore = retSo.getFrom_thickness();//修改前来料厚度
			Double toThicknessBefore = retSo.getTo_thickness();//修改前目标厚度
			if (isNotEmpty(iary.getWo_prty())) {
				retSo.setWo_prty(iary.getWo_prty());
			}
			if (isNotEmpty(iary.getWo_dsc())) {
				retSo.setWo_dsc(iary.getWo_dsc());
			}

			if (isNotEmpty(iary.getWo_cate())) {
				retSo.setWo_cate(iary.getWo_cate());
			}
			if (isNotEmpty(iary.getWo_prty())) {
				retSo.setWo_prty(iary.getWo_prty());
			}
			if (isNotEmpty(iary.getDest_shop())) {
				retSo.setDest_shop(iary.getDest_shop());
			}
			if (isNotEmpty(iary.getPln_stb_date())) {
				retSo.setPln_stb_date(Date.valueOf(iary.getPln_stb_date()));
			}
			if (isNotEmpty(iary.getPln_cmp_date())) {
				retSo.setPln_cmp_date(Date.valueOf(iary.getPln_cmp_date()));
				retSo.setPln_cmp_timestamp(Timestamp.valueOf(iary.getPln_cmp_date()+" 00:00:00"));
			}else{
				retSo.setPln_cmp_date(null);
				retSo.setPln_cmp_timestamp(null);
			}
			if (isNotEmpty(iary.getPln_prd_qty())) {
				retSo.setPln_prd_qty(Integer.parseInt(iary.getPln_prd_qty()));
			}
			if(iary.getTh_mdl_id_fk()!=null){
				retSo.setTh_mdl_id_fk(iary.getTh_mdl_id_fk());
			}
			if (iary.getFm_mdl_id_fk() != null) {// NO.01
				retSo.setFm_mdl_id_fk(iary.getFm_mdl_id_fk());
			}
			if (iary.getCut_mdl_id_fk() != null) {// NO.01
				retSo.setCut_mdl_id_fk(iary.getCut_mdl_id_fk());
			}
			if (iary.getWo_note() != null) {
				retSo.setWo_note(iary.getWo_note());
			}
			if (iary.getOpe_eff_flg() != null) {
				retSo.setOpe_eff_flg(iary.getOpe_eff_flg());
			}
			if (iary.getVcr_flg_fk() != null) {
				retSo.setVcr_flg_fk(iary.getVcr_flg_fk());
			}
			if(iary.getMtrl_prod_id()!=null){
				retSo.setMtrl_prod_id_fk(iary.getMtrl_prod_id());
			}
			if(isNotEmpty(iary.getCus_id())){
				retSo.setCus_id_fk(iary.getCus_id());
			}
			if(iary.getMtrl_part()!=null){
				retSo.setMtrl_part(iary.getMtrl_part());
			}
			if(iary.getT_thickness()!=null){
				retSo.setT_thickness(Double.valueOf(iary.getT_thickness()));
			}
			if(iary.getC_thickness()!=null){
				retSo.setC_thickness(Double.valueOf(iary.getC_thickness()));
			}
			if(null != iary.getManual()){
				retSo.setManual(iary.getManual());
			}
			if(null != iary.getCusMtrl()){
				retSo.setCus_mtrl_id_fk(iary.getCusMtrl());
			}
			if(null != iary.getGlassFrom()){
				retSo.setGlass_from(iary.getGlassFrom());
			}
			retSo.setEvt_usr(iary.getEvt_user());
			retSo.setEvt_timestamp(Util.getCurrentTimestamp());
			if (isNotEmpty(iary.getMdl_id())) {
				retSo.setMdl_id_fk(iary.getMdl_id());
				if (!retSo.getWo_cate().equals("RMA")) {
					String sql = "FROM Bis_pathi where id.path_id='" + iary.getPath_id() + "' and "
							+ "id.path_ver='" + iary.getPath_ver() + "'";
					Bis_pathi bis_pathi = (Bis_pathi) bisMdlDefDao.comTblRead(sql);
					if (bis_pathi == null) {
						xpaplysoO.setRtn_code(E_XPAPLYSO_BIS_PATH_NOT_FOUND + "");
						xpaplysoO.setRtn_mesg("没有找到工艺路线[" + iary.getPath_id()
								+ "],请到6100界面维护");
						return xpaplysoO;
					}
						retSo.setPath_id_fk(iary.getPath_id());
						retSo.setPath_ver_fk(iary.getPath_ver());
						retSo.setStr_ope_no(bis_pathi.getFirst_ope_no());
					retSo.setMdl_cate_fk(bis_pathi.getMdl_cate_fk());//订单的mdl_cate是从pathi中的mdl_cate获取
				}
			}
			retSo.setFrom_thickness(Double.valueOf(iary.getFrom_thickness()));//NO.02
			retSo.setTo_thickness(Double.valueOf(iary.getTo_thickness()));//NO.02

			retSo.setPlant_id("TODO");// TODO
			if(checkSoPlnPrdQty(retSo)==false){
				xpaplysoO.setRtn_code(E_XPAPLYSO_PLN_PRD_QTY_ERROR+"");
				xpaplysoO.setRtn_mesg("订单数量不能小于内部订单数量之和");
				return xpaplysoO;
			}
			retSo.setCus_info_fst(iary.getCus_info_fst());
			retSo.setCus_info_snd(iary.getCus_info_snd());
			retWODao.update(retSo);

			//若工艺路线变更,同步更新已绑定的内部订单的工艺路线
			String hql = "FROM Ret_wo where so_id = ?";
			List<Ret_wo> retWoList = retWODao.list(hql, soId);
			for(Ret_wo retWo : retWoList){
				retWo.setCus_id_fk(retSo.getCus_id_fk());
				retWo.setMtrl_prod_id_fk(retSo.getMtrl_prod_id_fk());
				retWo.setMdl_id_fk(retSo.getMdl_id_fk());
				retWo.setMdl_cate_fk(retSo.getMdl_cate_fk());
				retWo.setTh_mdl_id_fk(retSo.getTh_mdl_id_fk());
				retWo.setFm_mdl_id_fk(retSo.getFm_mdl_id_fk());
				retWo.setCut_mdl_id_fk(retSo.getCut_mdl_id_fk());
				retWo.setPath_id_fk(retSo.getPath_id_fk());
				retWo.setPath_ver_fk(retSo.getPath_ver_fk());
				retWo.setStr_ope_no(retSo.getStr_ope_no());
				retWo.setFrom_thickness(retSo.getFrom_thickness());
				retWo.setTo_thickness(retSo.getTo_thickness());
				retWo.setWo_prty(retSo.getWo_prty());
				retWo.setEvt_usr(iary.getEvt_user());
				retWo.setEvt_timestamp(Util.getCurrentTimestamp());
				retWo.setCus_info_fst(iary.getCus_info_fst());
				retWo.setCus_info_snd(iary.getCus_info_snd());
				retWo.setManual(iary.getManual());
				retWo.setCus_mtrl_id_fk(iary.getCusMtrl());
				retWo.setGlass_from(iary.getGlassFrom());
				retWo.setT_thickness(Double.valueOf(iary.getT_thickness()));
				retWo.setC_thickness(Double.valueOf(iary.getC_thickness()));
				retWo.setOpe_eff_flg(retSo.getOpe_eff_flg());
				retWo.setVcr_flg_fk(retSo.getVcr_flg_fk());
				retWo.setWo_cate(retSo.getWo_cate());
				retWODao.update(retWo);
				
				//来料厚度或者目标厚度发生变化的在制工单报警
//				if("W".equals(retWo.getWo_typ()) && retWo.getPln_prd_qty() != 0){
//					Double fromThicknessAfter = retWo.getFrom_thickness();//修改后来料厚度
//					Double toThicknessAfter = retWo.getTo_thickness();//修改后目标厚度
//					BigDecimal data1 = new BigDecimal(fromThicknessBefore); 
//					BigDecimal data2 = new BigDecimal(toThicknessBefore); 
//					BigDecimal data3 = new BigDecimal(fromThicknessAfter); 
//					BigDecimal data4 = new BigDecimal(toThicknessAfter); 
//					if(data1.compareTo(data3) !=0 ||
//						data2.compareTo(data4) != 0){
//						sendThicknessChangeFunc(retWo.getCus_id_fk(),retWo.getWo_id(),retWo.getSo_id(),
//								fromThicknessBefore,fromThicknessAfter,
//								toThicknessBefore,toThicknessAfter);
//					}
//				}
			}
		}
		xpaplysoO.setRtn_code("0000000");
		xpaplysoO.setRtn_mesg("success");
		return xpaplysoO;
	}

	public XpaplysoO getAllWoProc(XpaplysoI xpaplysoI) {
		XpaplysoO xpaplysoO = new XpaplysoO();
		String sql = "FROM Ret_wo where wo_typ='S' order by id.wo_id ";
		List<Ret_wo> woList = retWODao.find(sql);
		List<XpaplysoOA> oaryList = new ArrayList<XpaplysoOA>();
		if (woList != null) {
			for (Ret_wo ret_wo : woList) {
				XpaplysoOA oary = new XpaplysoOA();
				oary.setWo_id(ret_wo.getWo_id());
				oary.setWo_cate(ret_wo.getWo_cate());
				if (ret_wo.getPln_stb_date() != null) {
					oary.setPln_stb_date(ret_wo.getPln_stb_date().toString().substring(0, 10));
				}
				if (ret_wo.getPln_cmp_date() != null) {
					oary.setPln_cmp_date(ret_wo.getPln_cmp_date().toString().substring(0, 10));
				}
				oary.setWo_prty(ret_wo.getWo_prty());
				oary.setDest_shop(ret_wo.getDest_shop());
				oary.setMdl_id(ret_wo.getMdl_id_fk());
				oary.setPln_prd_qty(ret_wo.getPln_prd_qty());
				oary.setWo_dsc(ret_wo.getWo_dsc());
				oary.setTh_mdl_id_fk(ret_wo.getTh_mdl_id_fk());
				oary.setFm_mdl_id_fk(ret_wo.getFm_mdl_id_fk());// NO.01
				oary.setCut_mdl_id_fk(ret_wo.getCut_mdl_id_fk());// NO.01
				oary.setEvt_usr(ret_wo.getEvt_usr());
				oary.setCus_id(ret_wo.getCus_id_fk());
				if (ret_wo.getEvt_timestamp() != null) {
					oary.setEvt_timestamp(ret_wo.getEvt_timestamp().toString());
					oary.setOpe_eff_flg(ret_wo.getOpe_eff_flg());
				}
				//NO.02
				if (ret_wo.getFrom_thickness() != null) {
					oary.setFrom_thickness(ret_wo.getFrom_thickness().toString());
				}
				//NO.02
				if (ret_wo.getTo_thickness() != null) {
					oary.setTo_thickness(ret_wo.getTo_thickness().toString());
				}
				oary.setPath_id(ret_wo.getPath_id_fk());
				oary.setPath_ver(ret_wo.getPath_ver_fk());
				oaryList.add(oary);
			}
		}
		xpaplysoO.setRtn_code("0000000");
		xpaplysoO.setRtn_mesg("success");
		xpaplysoO.setOary(oaryList);
		xpaplysoO.setTbl_cnt(oaryList.size());

		return xpaplysoO;
	}

	public XpaplysoO  getSoForWoProc() {
		String hql;
		XpaplysoO xpaplysoO = new XpaplysoO();
		XpaplysoIA iary = inTrx.getIary().get(0);
		List<XpaplysoOA> oaryList = new ArrayList<XpaplysoOA>();
		try {
			
			if(Util.isSpaceCheck(iary.getSo_id())){
				hql = "FROM Ret_wo where wo_typ='W' and so_id<>'' and mtrl_prod_id_fk='"
					+ iary.getMtrl_prod_id() + "'";
			}else{
				hql = "FROM Ret_wo where wo_typ='W' and so_id='"
				    + iary.getSo_id()
				    + "' and mtrl_prod_id_fk='"
					+ iary.getMtrl_prod_id() + "'";
			}
			// and mdl_id_fk='"+ iary.getMdl_id() +"'";
			List<Ret_wo> retWOList = retWODao.find(hql);

			for (Ret_wo ret_wo : retWOList) {
				XpaplysoOA oary = new XpaplysoOA();
				oary.setWo_id(ret_wo.getWo_id());
				oary.setWo_cate(ret_wo.getWo_cate());
				if (ret_wo.getPln_stb_date() != null) {
					oary.setPln_stb_date(ret_wo.getPln_stb_date().toString().substring(0, 10));
				}
				if (ret_wo.getPln_cmp_date() != null) {
					oary.setPln_cmp_date(ret_wo.getPln_cmp_date().toString().substring(0, 10));
				}

				oary.setWo_prty(ret_wo.getWo_prty());
				oary.setDest_shop(ret_wo.getDest_shop());
				oary.setMdl_id(ret_wo.getMdl_id_fk());
				oary.setPln_prd_qty(comObj2Int(ret_wo.getPln_prd_qty()));
				oary.setTh_mdl_id_fk(ret_wo.getTh_mdl_id_fk());
				oary.setFm_mdl_id_fk(ret_wo.getFm_mdl_id_fk());// NO.01
				oary.setCut_mdl_id_fk(ret_wo.getCut_mdl_id_fk());// NO.01
				oary.setWo_dsc(ret_wo.getWo_dsc());
				oary.setEvt_usr(ret_wo.getEvt_usr());
				if (ret_wo.getEvt_timestamp() != null) {
					oary.setEvt_timestamp(ret_wo.getEvt_timestamp().toString());
				}
				// String hql = "FROM Bis_bom where id.mdl_id_fk ='" +
				// ret_wo.getMdl_id_fk()+"'" +
				// " and"
				oary.setMtrl_prod_id(ret_wo.getMtrl_prod_id_fk());
				oary.setWo_note(ret_wo.getWo_note());

				oaryList.add(oary);
			}
		} catch (Exception ex) {
			xpaplysoO.setRtn_code(RETURN_CODE_UNKNOWN);
			xpaplysoO.setRtn_mesg(ex.toString());
			return xpaplysoO;
		}

		xpaplysoO.setOary(oaryList);
		xpaplysoO.setTbl_cnt(oaryList.size());
		xpaplysoO.setRtn_code(RETURN_CODE_OK);
		xpaplysoO.setRtn_mesg(RETURN_MESG_OK);
		return xpaplysoO;
	}

	public void BindingSoForWoProc() {
		XpaplysoIA iary = inTrx.getIary().get(0);
		Ret_wo ret_so = retWODao.get(iary.getSo_id());
		Ret_wo ret_wo = retWODao.get(iary.getWo_id());

		if(WO_STATUS_CLOS.equals(ret_so.getWo_stat())){
			outTrx.setRtn_code(E_XPAPLYSO_CLOSE_STAT+"");
			outTrx.setRtn_mesg("订单已经被关闭,不可再做绑定,请去5100界面重新打开此订单");
			return ;
		}
		String mdlId = ret_so.getMdl_id_fk();
		Bis_mdl_def bisMdlDef = bisMdlDefDao.get(ret_so.getMdl_id_fk());
		String dqrInfo = bisMdlDef.getRsv_info(); 
		if(!StringUtils.isEmpty(dqrInfo) && dqrInfo.length() >= 3 && ( dqrInfo.charAt(0) == 'Y' || dqrInfo.charAt(2)=='Y' || bisMdlDef.getLayot_id_fk().contains("DQR"))){
			outTrx.setRtn_code(E_XPAPLYSO_OTHER_ERROR+"");
			outTrx.setRtn_mesg("产品["+mdlId + "]为DQR产品,要绑定内部订单请先解除DQR");
			return ;
		}
		if(!ret_so.getWo_cate().equals(ret_wo.getWo_cate())){
			outTrx.setRtn_code(E_XPAPLYSO_OTHER_ERROR+"");
			outTrx.setRtn_mesg("内部订单和商务订单的订单类型不同,无法绑定");
			return;
		}
		
		ret_wo.setSo_id(iary.getSo_id());
		ret_wo.setMdl_id_fk(ret_so.getMdl_id_fk());
		ret_wo.setMtrl_prod_id_fk(ret_so.getMtrl_prod_id_fk());
		ret_wo.setPath_id_fk(ret_so.getPath_id_fk());
		ret_wo.setPath_ver_fk(ret_so.getPath_ver_fk());
		ret_wo.setWo_cate(ret_so.getWo_cate());
		ret_wo.setWo_prty(ret_so.getWo_prty());
		ret_wo.setStr_ope_no(ret_so.getStr_ope_no());
		ret_wo.setEvt_usr(iary.getEvt_user());
		ret_wo.setEvt_timestamp(Util.getCurrentTimestamp());
//		ret_wo.setPln_stb_date(ret_so.getPln_stb_date());
//		ret_wo.setPln_cmp_date(ret_so.getPln_cmp_date());
//		ret_wo.setPln_stb_timestamp(ret_so.getPln_stb_timestamp());
//		ret_wo.setPln_cmp_timestamp(ret_so.getPln_cmp_timestamp());
		ret_wo.setDest_shop(ret_so.getDest_shop());
		ret_wo.setTh_mdl_id_fk(ret_so.getTh_mdl_id_fk());
		ret_wo.setFm_mdl_id_fk(ret_so.getFm_mdl_id_fk());// NO.01
		ret_wo.setCut_mdl_id_fk(ret_so.getCut_mdl_id_fk());// NO.01
		ret_wo.setFrom_thickness(ret_so.getFrom_thickness());
		ret_wo.setTo_thickness(ret_so.getTo_thickness());
		ret_wo.setCus_info_snd(ret_so.getCus_info_snd());
		ret_wo.setOpe_eff_flg(ret_so.getOpe_eff_flg());
		ret_wo.setCus_id_fk(ret_so.getCus_id_fk());
		ret_wo.setMdl_cate_fk(ret_so.getMdl_cate_fk());
		ret_wo.setCus_info_fst(ret_so.getCus_info_fst());
		ret_wo.setCus_info_snd(ret_so.getCus_info_snd());
		ret_wo.setT_thickness(ret_so.getT_thickness());
		ret_wo.setC_thickness(ret_so.getC_thickness());
		ret_wo.setSwh_cnt_flg(ret_so.getSwh_cnt_flg());
		ret_wo.setManual(ret_so.getManual());
		ret_wo.setCus_mtrl_id_fk(ret_so.getCus_mtrl_id_fk());
		ret_wo.setGlass_from(ret_so.getGlass_from());
		if(null != ret_so.getPln_cmp_timestamp()){
		    Timestamp newTime = Util.sec(ret_so.getPln_cmp_timestamp());
		    ret_wo.setPln_cmp_timestamp(newTime);
		    ret_wo.setPln_cmp_date(Date.valueOf(newTime.toString().substring(0, 10)));
		    ret_so.setPln_cmp_timestamp(newTime);
		}
		retWODao.update(ret_wo);
//		ret_so.setRl_prd_qty(ret_wo.getPln_prd_qty() + ret_so.getRl_prd_qty());
		ret_so.setRl_prd_qty(ret_wo.getRsv_pln_prd_qty() + ret_so.getRl_prd_qty());		
		if (ret_so.getRl_prd_qty() > ret_so.getPln_prd_qty()) {
			outTrx.setRtn_code(E_XPAPLYSO_OUT_OF_QTY + "");
			outTrx.setRtn_mesg("绑定该商务订单的所有内部定单计划来料数量总和[" + ret_so.getRl_prd_qty() + "]已大于商务订单计划来料数量["
					+ ret_so.getPln_prd_qty() + "],请修改商务订单计划来料数量");
			return;
			
		}
		retWODao.update(ret_so);
		
		
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);

	}

	public void bindrealSoForWoProc() {
		XpaplysoIA iary = inTrx.getIary().get(0);
		Ret_wo ret_wo = retWODao.get(iary.getWo_id());
		ret_wo.setSo_id(iary.getSo_id());
		ret_wo.setEvt_usr(iary.getEvt_user());
		ret_wo.setEvt_timestamp(Util.getCurrentTimestamp());
		retWODao.update(ret_wo);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);

	}


	public void getSoForWoByMtrlProc() {
		XpaplysoIA iary = inTrx.getIary().get(0);
//		String mtrl_prod_id = iary.getMtrl_prod_id();
//		String cus_id = iary.getCus_id();
		List<XpaplysoOA> oaryList = new ArrayList<XpaplysoOA>();
		String hql = "FROM Ret_wo where wo_typ='S' and wo_stat <> 'CLOS' ";
		if(iary.getMtrl_prod_id()!=null){
			hql = hql + "and  mtrl_prod_id_fk='" + iary.getMtrl_prod_id() + "'"; 
		}
		if(iary.getCus_id()!=null){
			hql = hql + "and  cus_id_fk='" + iary.getCus_id() + "'"; 
		}
		if(iary.getWo_cate()!=null){
			hql = hql + "and  wo_cate='" + iary.getWo_cate() + "'"; 
		}
		//		String hql = "FROM Ret_wo where wo_typ='S' and so_id is null and  mtrl_prod_id_fk='"
				
		List<Ret_wo> retWOList = retWODao.find(hql);
		for (Ret_wo ret_wo : retWOList) {
			XpaplysoOA oary = new XpaplysoOA();
			oary.setWo_id(ret_wo.getWo_id());
			oary.setWo_cate(ret_wo.getWo_cate());
			oary.setMdl_id(ret_wo.getMdl_id_fk());
			oary.setPln_prd_qty(comObj2Int(ret_wo.getPln_prd_qty()));
			oary.setWo_note(ret_wo.getWo_note());
			oary.setEvt_usr(ret_wo.getEvt_usr());

			oary.setWo_prty(ret_wo.getWo_prty());
			oary.setDest_shop(ret_wo.getDest_shop());
			oary.setTh_mdl_id_fk(ret_wo.getTh_mdl_id_fk());
			oary.setFm_mdl_id_fk(ret_wo.getFm_mdl_id_fk());
			oary.setCut_mdl_id_fk(ret_wo.getCut_mdl_id_fk());
			oary.setWo_dsc(ret_wo.getWo_dsc());
			oary.setPlant_id(ret_wo.getPlant_id());
			if(ret_wo.getEvt_timestamp()!=null){
				oary.setEvt_timestamp(ret_wo.getEvt_timestamp().toString());
			}
			oary.setPath_id(ret_wo.getPath_id_fk());
			if(ret_wo.getPln_stb_date()!=null){
				oary.setPln_stb_date(ret_wo.getPln_stb_date().toString());
			}
			if(ret_wo.getPln_cmp_date()!=null){
				oary.setPln_cmp_date(ret_wo.getPln_cmp_date().toString());
			}
			if(null!=ret_wo.getCus_info_fst()){
				oary.setCus_info_fst(ret_wo.getCus_info_fst());
			}
			if(null!=ret_wo.getCus_info_snd()){
				oary.setCus_info_snd(ret_wo.getCus_info_snd());
			}
			oaryList.add(oary);
		}
		outTrx.setTrx_id("XPAPLYSO");
		outTrx.setType_id("O");
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		outTrx.setTbl_cnt(oaryList.size());
		outTrx.setOary(oaryList);
	}

	private void closeWOProc() {
		String soID;
		Ret_wo ret_so = null;
		Timestamp currentTimeStamp = Util.getCurrentTimestamp();
		try {
			List<XpaplysoIA> iaryList = inTrx.getIary();
			for (XpaplysoIA iary : iaryList) {
				soID = iary.getWo_id();
				ret_so = retWODao.get(soID);
				ret_so.setWo_stat(WO_STATUS_CLOS);
				ret_so.setEvt_usr(iary.getEvt_user());
				ret_so.setEvt_timestamp(currentTimeStamp);
				ret_so.setAct_cmp_timestamp(currentTimeStamp);
				retWODao.update(ret_so);
			}
		} catch (Exception e) {
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(Util.stackTraceToString(e));
			return;
		}
		getWoProc();
//		outTrx.setRtn_code(RETURN_CODE_OK);
//		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}
	private void openWOProc() {

		String soID;
		Ret_wo ret_so = null;
		try {
			List<XpaplysoIA> iaryList = inTrx.getIary();
			for (XpaplysoIA iary : iaryList) {
				soID = iary.getWo_id();
				ret_so = retWODao.get(soID);
				ret_so.setWo_stat(WO_STATUS_WAIT);//TODO 是INPR还是WAIT
				ret_so.setEvt_usr(iary.getEvt_user());
				ret_so.setEvt_timestamp(Util.getCurrentTimestamp());
				retWODao.update(ret_so);
			}
		} catch (Exception e) {
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(Util.stackTraceToString(e));
			return;
		}
		getWoProc();
//		outTrx.setRtn_code(RETURN_CODE_OK);
//		outTrx.setRtn_mesg(RETURN_MESG_OK);
	
	}
	

	public String comObj2String(Object obj) {
		if (obj != null) {
			return obj.toString();
		}
		return null;
	}

	public int comObj2Int(Object obj) {
		if (obj != null) {
			return Integer.valueOf(obj.toString());
		}
		return 0;
	}

	public boolean isNotEmpty(Object obj) {
		if (obj != null && !obj.equals("")) {
			return true;
		}
		return false;
	}
	
	private void sendThicknessChangeFunc(String cus_id,String wo_id,String so_id,
			Double frThicknessBefore,Double frThicknessAfter,
			Double toThicknessBefore,Double toThicknessAfter){
		AmsthicknessI amsthicknessI = new AmsthicknessI();
		amsthicknessI.setTrx_id(T_AMSTHICKNESS);
		amsthicknessI.setType_id(TRX_IN);
		amsthicknessI.setCus_id(cus_id);
		amsthicknessI.setWo_id(wo_id);
		amsthicknessI.setSo_id(so_id);
		amsthicknessI.setFr_thickness_before(frThicknessBefore);
		amsthicknessI.setFr_thickness_after(frThicknessAfter);
		amsthicknessI.setTo_thickness_before(toThicknessBefore);
		amsthicknessI.setTo_thickness_after(toThicknessAfter);
		String strInMsg = mqOpe.obj2Str(amsthicknessI);
		sendMsgService.sendWithRepaly(strInMsg);//在制品厚度变更告警
	}
	
	private void queryCusSoProc(){
		List<Ret_cus_so> retCusSoList = new ArrayList<Ret_cus_so>();
		String sql="FROM Ret_cus_so WHERE CUS_SO_STAT<>'RELS'";
		retCusSoList = retCusSoDao.find(sql);
		outTrx.setOary_so(retCusSoList);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}
}
